from pyeasyaccept.gui.app.PythonEasyAcceptApp import PythonEasyAcceptApp
from pyeasyaccept.PythonEasyAcceptFacade import PythonEasyAcceptFacade
from pyeasyaccept.commom.FacadeFactory import FacadeFactory
from pyeasyaccept.commom.FindScript import FindScript
import os
import sys

LINE_SEPARATOR = os.linesep

def main(args):
    """
    It executes the Python EasyAccept application. It can run in two modes: text or graphic.
    """
    if len(args) == 0:
        runGUI()
    elif len(args) > 1:
        runTextMode(args)
    else:
        print "Syntax: " + LINE_SEPARATOR
        print "python PythonEasyAccept.py" + LINE_SEPARATOR, 1 * '\t', "Opens a Python EasyAccept GUI" + LINE_SEPARATOR
        print "python PythonEasyAccept.py Facade testScript1 testScript2 ... testScriptN" + LINE_SEPARATOR, 1 * '\t', "Runs the acceptance tests in text mode"
        
        
def runGUI():
    """
    Executes the Python EasyAccept Application in graphical mode.
    It takes no argument.
    """
    app = PythonEasyAcceptApp(0)
    app.MainLoop()
    
def getAllTestScripts(args):
    tests = []
    for arg in args:
        if os.path.isdir(arg):
            subTests = FindScript().getScripts(arg)
            for subTest in subTests:
                tests.append(subTest)
        else: tests.append(arg)
            
    return tests
    
def runTextMode(args):
    """
    Executes the Python EasyAccept Application in text mode.
    It receives the user application facade and the test scripts as argument.
    """
    facade = args[0]
    args.remove(facade)
    facade = os.path.abspath(facade)
    
    tests = getAllTestScripts(args)
    facadeInstance = FacadeFactory().getFacadeInstance(facade)
    peaFacade = PythonEasyAcceptFacade(facadeInstance, tests)
    peaFacade.executeTests()
    print peaFacade.getCompleteResults()
    
if __name__ == '__main__':
    if len(sys.argv) > 0:
        main(sys.argv[1:])
    else: 
        main([])